<template>
	<div class="my_l">
		<header class="top_bar">
			<a onclick="window.history.go(-1)" class="icon_back"></a>
			<h3 class="cartname">京东登录</h3>
		</header>
		<main class="user_login_box">
			<div class="login_dialog">
				<div class="_username">
					<input @change="changeVal" type="text" placeholder="请输入用户名" class="user_input" v-model="username" />
				</div>
				<div class="_username u_passwd">
					<input type="password" placeholder="请输入密码" class="user_input" v-model="password" />
				</div>

				<div class="login_box">
					<a @click="goLogin()" class="btn_login">登录</a>
				</div>
				<div class="login_box">
					<a @click="zhifu()" class="btn_login">支付</a>
				</div>
				<div class="go_reg_box">
					<router-link tag="span" to="/register">快速注册</router-link>
				</div>
			</div>
		</main>
	</div>
</template>
<script>
	import { Toast } from 'mint-ui';

	export default {
		created() {
			console.log('this.username', this.username)
			console.log('this.$store.state.user_name', this.$store.state.user_name)
		},
		data() {
			return {
				username: '',
				password: '',
				userInfo: {}
			}
		},
		methods: {
			goLogin() {
				let _this = this;

				if(_this.username == '') {
					alert('请输入用户名');
				} else if(_this.password == '') {
					alert('请输入密码');
				} else {
					_this.$http.post('/base/login', {
						CellPhone: _this.username,
						Password: _this.password,
					}, undefined, res => {
						console.log('登录res', res)
						if(res.code === 200) {
							_this.$store.state.user_name = _this.username
							console.log('this.username0', this.username)
							console.log('this.$store.state.user_name0', this.$store.state.user_name)
							window.localStorage.Authorization = res.data;
							console.log('window.localStorage.Authorization', window.localStorage.Authorization)
							Toast('登录成功！');
							setTimeout(function() {
								_this.$router.push({
									path: '/'
								});
							}, 1500)

						} else {
							Toast(res.error);

						}
					})
				}

			},
			changeVal() {
				this.$store.state.user_name = this.username
			},
			zhifu() {
				function onBridgeReady() {
					WeixinJSBridge.invoke(
						'getBrandWCPayRequest', {
							"appId": "wxf6c20e20fb476316", //公众号名称，由商户传入     
							"timeStamp": "1395712654", //时间戳，自1970年以来的秒数     
							"nonceStr": "e61463f8efa94090b1f366cccfbbb444", //随机串     
							"package": "prepay_id=u802345jgfjsdfgsdg888",
							"signType": "MD5", //微信签名方式：     
							"paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
						},
						function(res) {
							if(res.err_msg == "get_brand_wcpay_request:ok") {} // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
						}
					);
				}
				if(typeof WeixinJSBridge == "undefined") {
					alert('a')
					if(document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
					} else if(document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
						document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
					}
				} else {
					onBridgeReady();
				}

			}
		}
	}
</script>
<style>
	@import '../assets/css/login.css';
</style>